"""
通过 excel 对应的病历号找到对应的文件并存放到指定的文件夹
"""
import glob
import os
import pandas as pd
import shutil

from tqdm import tqdm

# 读取Excel文件
df = pd.read_excel('/media/hsmy/16TB/20240724GIST/文件名对应病理号111.xlsx')

# 假设我们要根据'ColumnA'列的值作为key，找到对应的'ColumnB'和'ColumnC'的值
# 首先，我们需要将DataFrame转换为字典列表，其中每个字典代表一行数据
dict_list = df.to_dict(orient='records')

lack_wsi_arr = []
wsi_folder = "/media/hsmy/GIST-1/smu/新建文件夹/"
target_move_folder = "/media/hsmy/16TB/20240724GIST/GIST_SM"
for row in tqdm(dict_list):
    file_name = str(row['文件名'])
    pa = "PA" + str(row['病理号']) + "_" + str(row['小号']) + ".svs"
    p1 = os.path.join(wsi_folder, f'*{file_name}*.svs')
    files = glob.glob(p1)
    if len(files) == 0:
        lack_wsi_arr.append(pa)
    else:
        # print(pa)
        for wsi in files:
            out_path = os.path.join(target_move_folder, pa)
            shutil.copy(wsi, out_path)

print(len(lack_wsi_arr))
pd.DataFrame(lack_wsi_arr).to_csv('miss_sm.csv', index=None, header=None)
